package cn.edu.xhu.library_management.area.service.impl;

import cn.edu.xhu.library_management.area.entity.LmArea;
import cn.edu.xhu.library_management.area.entity.LmItemNum;
import cn.edu.xhu.library_management.area.mapper.AreaMapper;
import cn.edu.xhu.library_management.area.mapper.ItemNumMapper;
import cn.edu.xhu.library_management.area.service.ItemNumService;
import cn.edu.xhu.library_management.common.entity.ResultCode;
import cn.edu.xhu.library_management.common.exception.LibraryManagementException;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * @Author: MMJ
 * @Date: 2022 - 11 - 18
 * @Description:
 */
@Service
public class ItemNumServiceImpl implements ItemNumService {
    @Resource
    private ItemNumMapper itemNumMapper;
    @Resource
    private AreaMapper areaMapper;
    @Override
    public LmItemNum findItemNumsByAreaId(Integer areaId) {
        return itemNumMapper.findItemNumByAreaId(areaId);
    }

    @Override
    public boolean addItemNum(LmItemNum itemNum) {
        // 查询区域id是否存在
        LmArea area = areaMapper.findAreaById(itemNum.getAreaId());
        if (area == null){
            throw new LibraryManagementException(ResultCode.OPERATION_ERROR,"区域id有误");
        }
        return itemNumMapper.addItemNum(itemNum) > 0;
    }

    @Override
    public boolean updateItemNum(LmItemNum itemNum) {
        // 查询物品数量Id，是否存在
        LmItemNum lmItemNum = itemNumMapper.findItemNumById(itemNum.getId());
        if (lmItemNum == null){
            throw new LibraryManagementException(ResultCode.OPERATION_ERROR,"物品数量Id有误");
        }
        // 判断区域Id 是否存在
        LmArea area = areaMapper.findAreaById(itemNum.getAreaId());
        if (area == null){
            throw new LibraryManagementException(ResultCode.OPERATION_ERROR,"区域id有误");
        }
        return itemNumMapper.updateItemNum(itemNum) > 0;
    }

    @Override
    public boolean deleteItemNumById(Integer id) {
        return itemNumMapper.deleteItemNumById(id) > 0;
    }
}
